package com.basics.channel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basics.api.response.channel.GroupListData;
import com.basics.api.response.organization.GroupOrganizationListData;
import com.basics.channel.entity.CyChannelGroup;
import com.basics.channel.mapper.CyChannelGroupMapper;
import com.basics.channel.service.CyChannelGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 频道分组 服务实现类
 * </p>
 *
 * @author WangWei
 * @since 2025-03-09
 */
@Service
public class CyChannelGroupServiceImpl extends ServiceImpl<CyChannelGroupMapper, CyChannelGroup> implements CyChannelGroupService {

    @Override
    public List<GroupListData> selectChannelGroupList(Long channelId){
        List<CyChannelGroup> groupList = list(new QueryWrapper<CyChannelGroup>()
                .eq("del_flag",0)
                .eq("channel_id",channelId).orderByAsc("sort_by"));
        List<GroupListData> resultList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(groupList)){
            groupList.forEach(v->{
                GroupListData data = new GroupListData();
                data.setId(v.getId());
                data.setGroupName(v.getGroupName());
                resultList.add(data);
            });
        }
        return resultList;
    }

    @Override
    public  List<GroupOrganizationListData> selectGroupList(Long channelId){
        return baseMapper.selectGroupList(channelId);
    }

    @Override
    public CyChannelGroup getDefaultGroup(Long channelId){
        return getOne(new QueryWrapper<CyChannelGroup>()
                .eq("del_flag",0)
                .eq("channel_id",channelId)
                .eq("default_flag",1));
    }

    public String selectByIds(String groupIds){
        if(groupIds==null){
            return "";
        }
        Collection<CyChannelGroup> groupList = listByIds(Arrays.asList(groupIds.split(",")));
        List<String> groupNames = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(groupList)){
            groupList.forEach(v->{
                groupNames.add(v.getGroupName());
            });
        }
        return String.join(",",groupNames);
    }
}
